package com.eagle.roomcrawler.controller;


import com.eagle.roomcrawler.entity.RoomInfo;
import com.eagle.roomcrawler.mapper.RoomInfoMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.common.lucene.search.function.FieldValueFactorFunction;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.functionscore.FieldValueFactorFunctionBuilder;
import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder;
import org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders;
import org.elasticsearch.search.sort.FieldSortBuilder;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Optional;

@Slf4j
@RestController
public class ElasticSearchRoomController {

    @Autowired
    private RoomInfoMapper roomInfoMapper;


    @GetMapping("/get/book/novel")
    public ResponseEntity get(){

        return new ResponseEntity(null, HttpStatus.OK);
    }

    /**
     * 1、查  id
     * @param id
     * @return
     */
    @GetMapping("/get/{id}")
    public Optional<RoomInfo> getBookById(@PathVariable String id) {
        long id1 = Long.parseLong(id);
        return roomInfoMapper.findById(id1);
    }

    /**
     * 查询所有数据
     * @return
     */
    @RequestMapping(value="/getRooms",method = RequestMethod.GET)
    @ResponseBody
    public Iterable<RoomInfo> getAll(){

        return roomInfoMapper.findAll();
//        return roomInfoList;
    }

    /**
     * 3、查   +++：分页、分数、分域（结果一个也不少）
     * @param page
     * @param size
     * @param q
     * @return
     * @return
     */
    @GetMapping("/{page}/{size}/{q}")
    public Page<RoomInfo> searchCity(@PathVariable Integer page, @PathVariable Integer size, @PathVariable String q) {

//        // 分页参数
        Pageable pageable = new PageRequest(page-1, size);
//
//        // 分数，并自动按分排序
//
//        FieldValueFactorFunctionBuilder title = ScoreFunctionBuilders.fieldValueFactorFunction("title").setWeight(1000);
//        FieldValueFactorFunctionBuilder addr = ScoreFunctionBuilders.fieldValueFactorFunction("addr").setWeight(100);
//        FunctionScoreQueryBuilder.FilterFunctionBuilder[] filterFunctionBuilders = new FunctionScoreQueryBuilder.FilterFunctionBuilder[]{new FunctionScoreQueryBuilder.FilterFunctionBuilder(title),new FunctionScoreQueryBuilder.FilterFunctionBuilder(addr)};
//        FunctionScoreQueryBuilder functionScoreQueryBuilder = QueryBuilders.functionScoreQuery(filterFunctionBuilders);
//
//        //过时了
////        FunctionScoreQueryBuilder functionScoreQueryBuilder = QueryBuilders.functionScoreQuery()
////                .add(QueryBuilders.boolQuery().should(QueryBuilders.matchQuery("title", q)),
////                ScoreFunctionBuilders.weightFactorFunction(1000)) // 权重："title" 1000
////                .add(QueryBuilders.boolQuery().should(QueryBuilders.matchQuery("addr", q)),
////                        ScoreFunctionBuilders.weightFactorFunction(100)); // 权重：addr 100分
//
//        //排序
//        FieldSortBuilder sort = SortBuilders.fieldSort("id").order(SortOrder.DESC);
//        // 分数、分页
//        SearchQuery searchQuery = new NativeSearchQueryBuilder().withPageable(pageable)
//                .withSort(sort)
//                .withQuery(functionScoreQueryBuilder).build();
//
//        Page<RoomInfo> searchPageResults = roomInfoMapper.search(searchQuery);
//        return searchPageResults.getContent();

        //排序
        FieldSortBuilder sort = SortBuilders.fieldSort("id").order(SortOrder.DESC);
        BoolQueryBuilder boolQuery = QueryBuilders.boolQuery().should(QueryBuilders.matchQuery("title", q));

        SearchQuery searchQuery = new NativeSearchQueryBuilder().withPageable(pageable)
                .withSort(sort)
                .withQuery(boolQuery).build();

        Page<RoomInfo> searchPageResults = roomInfoMapper.search(searchQuery);
        return searchPageResults;
    }

}
